package com.zwk.center.controller;


import com.zwk.base.entity.CommonResult;
import com.zwk.base.util.JwtUtils;
import com.zwk.center.entity.UcenterMember;
import com.zwk.center.entity.vo.RegisterVo;
import com.zwk.center.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zwk
 * @since 2022-08-28
 */
@RestController
@CrossOrigin
@RequestMapping("/center/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    // 登录
    @PostMapping("/login")
    public CommonResult loginUser(@RequestBody UcenterMember member){

        // 返回token
        String token = ucenterMemberService.login(member);
        return CommonResult.ok().data("token", token);
    }

    // 注册
    @PostMapping("/register")
    public CommonResult registerUser(@RequestBody RegisterVo registerVo){

        ucenterMemberService.register(registerVo);

        return CommonResult.ok();
    }

    @GetMapping("/getMemberInfo")
    public CommonResult getMemberInfo(HttpServletRequest request){

        // 调用Jwt工具类, 根据request对象获取用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        // 查询数据
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        return CommonResult.ok().data("userInfo", ucenterMember);
    }

    // 通过用户id获取用户数据, 由其他模块调用
    @PostMapping("/getMemberInfoById")
    public CommonResult getMemberInfoById(@RequestBody String id){
        System.out.println(id);
        UcenterMember member = ucenterMemberService.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("memberId", member.getId());
        map.put("nickname", member.getNickname());
        map.put("avatar", member.getAvatar());
        map.put("mobile", member.getMobile());
        return CommonResult.ok().data(map);
    }

    // 查询某一天的注册人数
    @GetMapping("/countRegister/{day}")
    public CommonResult countRegister(@PathVariable("day") String day){
        Integer count = ucenterMemberService.countRegisterDay(day);
        return CommonResult.ok().data("countRegister", count);
    }

}

